<?php
if (!isset($_POST) || !count($_POST)) {
echo <<<BLOCK
<!doctype html>
<head>
<title>Web Worker Wars - JavaScript AI Game</title>
<style>
* {
    font-family: Arial;
    line-height: 1.5em;
}
div {
    margin: 0 auto;
    width: 960px;
}
</style>
</head>
<body>
<div>
<h1>Web Worker Wars</h1>

<p>
Web Worker Wars is a game for JavaScript programmers written, of course, in JavaScript.
Something like a Google AI Challenge or HabraWars.<br/>
<a href="http://habrahabr.ru/blogs/programmers_games/110540/">More info</a> (Article in Russian) <a href="http://code.google.com/p/web-worker-wars/">Project on Google Code</a>
</p>
<hr />
<form action="" method="post">
<h2>Battle Editor</h2>
<p>Please input bot code id from pastebin.com URL: http://pastebin.com/<u>ZRV1vYGY</u></p>
<p>Only Valid JavaScript Web Worker code possible <a href="http://code.google.com/p/web-worker-wars/">How to make WWWs bot</a></p>
Bot 1: <input name="bot[]" type="text" value="ZRV1vYGY"/><br />
Bot 2: <input name="bot[]" type="text" value="ZRV1vYGY"/><br />
<p><input type="submit" value="Fight!"/></p>
</form>
<p>Simple bot code</p>
<iframe src="http://pastebin.com/embed_iframe.php?i=ZRV1vYGY" style="border:1px solid #000;width:100%;height:400px;"></iframe>

<div id="disqus_thread" style="width: 960px;"></div>
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'azproduction'; // required: replace example with your forum shortname

    // The following are highly recommended additional parameters. Remove the slashes in front to use.
    // var disqus_identifier = 'unique_dynamic_id_1234';
    var disqus_url = 'http://azproduction.ru/web_worker_wars/';

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</body>
BLOCK;
} else {
    if (count($_POST['bot'])) {
        for ($i = 0; $i < count($_POST['bot']); $i++) {
            if (!preg_match("/^[A-Za-z0-9]{8,20}$/", $_POST['bot'][$i])) {
                die('Wrong pastebin.com code');
            }
        }
        $bots = array_unique($_POST['bot']);
        for ($i = 0; $i < count($bots); $i++) {
            if (!file_exists('./js/workers/' . $bots[$i] . '.js')) {
                $content = @file_get_contents("http://pastebin.com/download.php?i={$bots[$i]}");
                if ($content === false) {
                    die('Error while uploading ' . $bots[$i]);
                } else {
                    file_put_contents('./js/workers/' . $bots[$i] . '.js', $content);
                }
            }
        }
        header("Location: arena.html#{$_POST['bot'][0]}&{$_POST['bot'][1]}");
    } else {
        die('Hmm');
    }
}
?>